//
//  STSilentLivenessView.m
//  FinanceDemo
//
//  Created by zhanghenan on 2018/7/24.
//  Copyright © 2018年 sensetime. All rights reserved.
//

#import "STSilentLivenessView.h"
#import "STSilentLivenessCommon.h"
NSInteger const STSilentLivenessLabelFrontSize = 15;

@interface STSilentLivenessView ()

@end

@implementation STSilentLivenessView

- (instancetype)initWithFrame:(CGRect)frame windowFrame:(CGRect)windowFrame {
    self = [super initWithFrame:frame];
    if (!self) {
        return self;
    }
    self.windowFrame = windowFrame;
    self.contentMode = UIViewContentModeScaleAspectFill;
    self.clipsToBounds = YES;
    self.autoresizesSubviews = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; //

    _maskCoverView = [[SilentLivenessMaskView alloc] initWithFrame:self.bounds];
    _maskCoverView.windowRect = self.windowFrame;
    [self addSubview:_maskCoverView];
    _hintLabel = [[UILabel alloc] init];
    [_hintLabel setBackgroundColor:[UIColor clearColor]];
    [_hintLabel setTextColor:kSTColorWithRGB(0xc7c7c7)];
    [_hintLabel setFont:[UIFont systemFontOfSize:STSilentLivenessLabelFrontSize]];
    [_hintLabel setFrame:CGRectMake(0, 0, kSTScreenWidth, 15)];

    if (IS_IPHONE) {
        _hintLabel.center =
            CGPointMake(CGRectGetMidX(self.windowFrame), self.windowFrame.origin.y + self.windowFrame.size.height + 30);
    } else {
        _hintLabel.center = CGPointMake(CGRectGetMidX(self.windowFrame), self.windowFrame.origin.y - 20);
    }

    [_hintLabel setTextAlignment:NSTextAlignmentCenter];
    [_hintLabel setNumberOfLines:0];

    [self addSubview:_hintLabel];

    return self;
}

- (CGFloat)modifyYRatio {
    CGFloat videoRatio = 720.0 / 1280.0;
    CGFloat uiRatio = CGRectGetWidth(self.bounds) / CGRectGetHeight(self.bounds);
    return uiRatio / videoRatio;
}

@end

@interface SilentLivenessMaskView ()

@end

@implementation SilentLivenessMaskView {
    CGContextRef context;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _lineColor = kSTColorWithRGB(0x777777);
        _maskAlpha = 1.0;
        _maskColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:_maskAlpha];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    context = UIGraphicsGetCurrentContext(); //! OCLINT
    CGFloat redColor = 12;
    CGFloat blueColor = 12;
    CGFloat greenColor = 12;
    [self.maskColor getRed:&redColor green:&greenColor blue:&blueColor alpha:nil];

    UIColor *maskColor = [UIColor colorWithRed:redColor green:greenColor blue:blueColor alpha:self.maskAlpha];
    [maskColor setFill];
    CGContextFillRect(context, self.bounds);
    CGContextClearRect(context, self.windowRect);

    CGFloat fLineWidth = 2;

    CGContextMoveToPoint(context, self.windowRect.origin.x, self.windowRect.origin.y);

    CGContextAddLineToPoint(context, self.windowRect.origin.x + self.windowRect.size.width, self.windowRect.origin.y);
    CGContextAddLineToPoint(context,
                            self.windowRect.origin.x + self.windowRect.size.width,
                            self.windowRect.origin.y + self.windowRect.size.height);
    CGContextAddLineToPoint(context, self.windowRect.origin.x, self.windowRect.origin.y + self.windowRect.size.height);
    CGContextAddLineToPoint(context, self.windowRect.origin.x, self.windowRect.origin.y);
    CGContextClosePath(context);
    [self.lineColor set];
    CGContextSetLineWidth(context, fLineWidth);
    CGContextStrokePath(context);
}
@end
